import { ReactNode } from 'react';

/**
 * 组件类型枚举 - 定义了所有可用的组件类型
 */
export enum ComponentType {
  // 数据展示组件
  TABLE = 'table',
  LIST = 'list',
  CHART = 'chart',
  
  // 表单组件
  FORM = 'form',
  INPUT = 'input',
  SELECT = 'select',
  RADIO = 'radio',
  CHECKBOX = 'checkbox',
  DATE_PICKER = 'datePicker',
  
  // 布局组件
  CONTAINER = 'container',
  GRID = 'grid',
  TABS = 'tabs',
  
  // 操作组件
  BUTTON = 'button',
  SEARCH_BAR = 'searchBar',
  
  // 其他组件
  IMAGE = 'image',
  TEXT = 'text',
  DIVIDER = 'divider',
  
  // 新增组件
  LAYOUT = 'layout',
  DISPLAY = 'display',
  CUSTOM = 'custom',
}

/**
 * 组件分类枚举
 */
export enum ComponentCategory {
  DATA_DISPLAY = '数据展示',
  FORM = '表单组件',
  LAYOUT = '布局组件',
  ACTION = '操作组件',
  OTHERS = '其他组件',
  
  // 新增分类
  BASIC = 'basic',
  DATA = 'data',
  ADVANCED = 'advanced',
}

/**
 * 组件属性类型
 */
export enum PropType {
  STRING = 'string',
  NUMBER = 'number',
  BOOLEAN = 'boolean',
  OBJECT = 'object',
  ARRAY = 'array',
  FUNCTION = 'function',
  DATE = 'date',
  COLOR = 'color',
  SELECT = 'select',
  MODEL_FIELD = 'modelField',
  
  // 新增属性类型
  ICON = 'icon',
  EXPRESSION = 'expression',
  EVENT = 'event',
  STYLE = 'style',
  COMPONENT = 'component',
}

/**
 * 组件属性定义
 */
export interface PropDefinition {
  name: string;
  label: string;
  type: PropType;
  defaultValue?: unknown;
  required?: boolean;
  options?: Array<{ label: string; value: unknown }>;
  bindable?: boolean; // 是否可绑定数据模型
}

/**
 * 组件样式定义
 */
export interface ComponentStyle {
  width?: string | number;
  height?: string | number;
  padding?: string | number;
  margin?: string | number;
  color?: string;
  backgroundColor?: string;
  fontSize?: string | number;
  fontWeight?: string | number;
  borderRadius?: string | number;
  border?: string;
  boxShadow?: string;
  textAlign?: 'left' | 'center' | 'right';
  display?: string;
  flexDirection?: 'row' | 'column';
  justifyContent?: string;
  alignItems?: string;
  [key: string]: unknown; // 其他样式属性
}

/**
 * 组件事件类型
 */
export enum EventType {
  CLICK = 'click',
  CHANGE = 'change',
  SUBMIT = 'submit',
  FOCUS = 'focus',
  BLUR = 'blur'
}

/**
 * 组件事件定义
 */
export interface ComponentEvent {
  type: EventType;
  handler: string; // 事件处理函数代码或引用
  params?: Record<string, unknown>; // 事件参数
}

/**
 * 组件数据绑定
 */
export interface DataBinding {
  model: string; // 模型名称
  fields: Record<string, string>; // 字段映射 {组件属性: 模型字段}
}

/**
 * 组件布局定义
 */
export interface ComponentLayout {
  x: number; // 网格 X 坐标
  y: number; // 网格 Y 坐标
  w: number; // 宽度(网格单位)
  h: number; // 高度(网格单位)
  minW?: number; // 最小宽度
  minH?: number; // 最小高度
  maxW?: number; // 最大宽度
  maxH?: number; // 最大高度
  static?: boolean; // 是否静态(不可移动)
}

/**
 * 组件显示条件
 */
export interface ComponentCondition {
  field: string; // 字段路径, 如: "form.name", "global.userType" 
  operator: '==' | '!=' | '>' | '<' | '>=' | '<=' | 'contains' | 'isEmpty' | 'isNotEmpty';
  value?: unknown; // 比较值
  and?: ComponentCondition[]; // AND条件组
  or?: ComponentCondition[]; // OR条件组
}

/**
 * 组件实例定义
 */
export interface Component {
  id: string;
  name?: string;
  type: ComponentType;
  props: Record<string, unknown>;
  layout: ComponentLayout;
  styles?: ComponentStyle;
  events?: ComponentEvent[];
  dataBinding?: DataBinding;
  conditions?: ComponentCondition[]; // 显示条件
  children?: Component[]; // 子组件(用于容器类组件)
}

/**
 * 组件注册定义(物料)
 */
export interface ComponentDefinition {
  type: ComponentType;
  name: string;
  title: string;
  icon?: string;
  category: ComponentCategory;
  properties: PropDefinition[];
  defaultProps?: Record<string, unknown>;
  defaultLayout: {
    w: number;
    h: number;
    minW?: number;
    minH?: number;
  };
  allowedChildren?: string[];
  maxChildren?: number;
  minChildren?: number;
  render: (props: any) => ReactNode;
  preview?: (props: any) => ReactNode;
  initialize?: () => Record<string, any>;
}

// 基础组件类型定义
export const BasicComponentTypes = {
  BUTTON: 'button',
  INPUT: 'input',
  SELECT: 'select',
  TABLE: 'table',
  FORM: 'form',
} as const;

export type BasicComponentType = typeof BasicComponentTypes[keyof typeof BasicComponentTypes];